/*
 * Copyright (c) 2022 Diemit <598757652@qq.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include "gpio_types.h"
#include "gpio_if.h"
#include "cmsis_os2.h"
#include "hdf_log.h"
#include "ohos_run.h"

#define SYS_DELAY_TICKS 200
#define TASK_STACK_SIZE 4096
#define TASK_PRIO 25

#define LED_OFF 0
#define LED_ON  1

// 在hcs文件中对应具体的GPIO口
#define M5EPD_KEY_LEFT_PIN 0      // 独立按键L
#define M5EPD_KEY_PUSH_PIN 1      // 独立按键P
#define M5EPD_KEY_RIGHT_PIN 2      // 独立按键R

osThreadId_t g_taskID = NULL;

/**
 * @brief 中断回调函数
 *
 */
static int32_t GpioIrqCallback(uint16_t gpio, int data)
{
    (void *)data;
    HDF_LOGE("---> hdf gpio index %d intterrupt!!", gpio);
}

static void button_test(void)
{
    uint16_t key_state1 = 0;
    uint16_t key_state2 = 0;
    uint16_t key_state3 = 0;
    GpioSetDir(M5EPD_KEY_LEFT_PIN, GPIO_DIR_IN);  // 按键配置为输入
    GpioSetDir(M5EPD_KEY_PUSH_PIN, GPIO_DIR_IN);  
    GpioSetDir(M5EPD_KEY_RIGHT_PIN, GPIO_DIR_IN);  
    GpioSetIrq(M5EPD_KEY_LEFT_PIN, GPIO_IRQ_TRIGGER_FALLING, (GpioIrqFunc)GpioIrqCallback, NULL); // 设置中断、上升下降沿触发
    GpioSetIrq(M5EPD_KEY_PUSH_PIN, GPIO_IRQ_TRIGGER_FALLING, (GpioIrqFunc)GpioIrqCallback, NULL); 
    GpioSetIrq(M5EPD_KEY_RIGHT_PIN, GPIO_IRQ_TRIGGER_FALLING, (GpioIrqFunc)GpioIrqCallback, NULL); 

    GpioEnableIrq(M5EPD_KEY_LEFT_PIN);            // 使能中断
    GpioEnableIrq(M5EPD_KEY_PUSH_PIN);
    GpioEnableIrq(M5EPD_KEY_RIGHT_PIN);
    while (1) {
        GpioRead(M5EPD_KEY_LEFT_PIN, &key_state1); // 读取GPIO状态
        GpioRead(M5EPD_KEY_PUSH_PIN, &key_state2);
        GpioRead(M5EPD_KEY_RIGHT_PIN, &key_state3);
        printf("--> The ButtonL has been %s\r\n", key_state1 ? "released" : "pressed");
        printf("--> The ButtonP has been %s\r\n", key_state2 ? "released" : "pressed");
        printf("--> The ButtonR has been %s\r\n", key_state3 ? "released" : "pressed");
        osDelay(SYS_DELAY_TICKS);
    }
    GpioUnsetIrq(M5EPD_KEY_LEFT_PIN, NULL);
    GpioUnsetIrq(M5EPD_KEY_PUSH_PIN, NULL);
    GpioUnsetIrq(M5EPD_KEY_RIGHT_PIN, NULL);
    GpioDisableIrq(M5EPD_KEY_LEFT_PIN);
    GpioDisableIrq(M5EPD_KEY_PUSH_PIN);
    GpioDisableIrq(M5EPD_KEY_RIGHT_PIN);
}

static void button_task(void)
{
    HDF_LOGE("into button test!\n");
    osThreadAttr_t attr;
    attr.name = "button_test";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = TASK_STACK_SIZE;
    attr.priority = TASK_PRIO;
    g_taskID = osThreadNew((osThreadFunc_t)button_test, NULL, &attr);
    if (g_taskID == NULL) {
        HDF_LOGE("Falied to create Test Button thread!\n");
    }
}

OHOS_APP_RUN(button_task);
